# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

# Use the right mkspecs file
if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
  _platform=win32-clang-g++
else
  _platform=win32-g++
fi

_realname=qt5-static
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.15.9
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
pkgdesc="A cross-platform application and UI framework (mingw-w64-static)"
url='https://www.qt.io/'
license=('GPL3' 'LGPL' 'FDL' 'custom')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-vulkan"
         "${MINGW_PACKAGE_PREFIX}-z3")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
             "${MINGW_PACKAGE_PREFIX}-fxc2"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-make"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-dbus"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "m4"
             "perl")
groups=("${MINGW_PACKAGE_PREFIX}-qt-static" "${MINGW_PACKAGE_PREFIX}-qt5-static")
_pkgfile="qt-everywhere-opensource-src-${pkgver}"
_pkgfqn="qt-everywhere-src-${pkgver}"
noextract=(${_pkgfile}.tar.xz)
source=(https://download.qt.io/official_releases/qt/${pkgver%.*}/${pkgver}/single/${_pkgfile}.tar.xz
        0000-adjust-qmake-conf-mingw.patch
        0001-qt-5.8.0-fix-sql-libraries-mingw.patch
        0002-qt-5.8.0-configure-gcc-before-clang.patch
        0003-enable-avx-support.patch
        0004-fix-linking-again-different-static-libs.patch
        0006-qt-5.3.0-win_flex-replace.patch
        0007-qt-5.3.0-win32-g-Enable-static-builds.patch
        0008-qt-5.3.0-win32-g-Add-QMAKE_EXTENSION_IMPORTLIB-defaulting-to-.patch
        0011-qt-5.8.0-mingw-dbus-and-pkg-config.patch
        0016-qt-5.8.0-win32-g++-use-qpa-genericunixfontdatabase.patch
        0017-qt-5.3.0-fix-examples-building.patch
        0024-qt-5.3.0-icu-add-U_LIB_SUFFIX_C_NAME.patch
        0025-qt-5.8.0-force-using-make-on-msys.patch
        0028-qt-5.8.0-Revert-untangle-use-of-system-vs.-shell-path-list-se.patch
        0029-qt-5.8.0-Revert-fix-quoting-and-path-separators-in-qtPrepareT.patch
        0030-qt-5.3.1-workaround-ansidecl-h-PTR-define-conflict.patch
        0035-qt-5.3.2-dont-add-resource-files-to-qmake-libs.patch
        0036-qt-5.3.2-win32-qt5-static-cmake-link-ws2_32-and--static.patch
        0046-qt-5.4.1-Revert-Revert-fix-NTFS-mount-points.patch
        0048-qt-5.4.2-win32-Avoid-platformNativeInterface-segfaults-with-minimal-platform.patch
        0049-qt-5.8.0-win32-do-not-use-fontconfig.patch
        0051-qt-5.9.1-disable-qtlocation-mapboxgl-plugin.patch
        0052-qt-5.11-mingw-fix-link-qdoc-with-clang.patch
        0054-win32-dont-need-flatpack-theme.patch
        0056-qt-5.11-static_icu.patch
        0057-fix-and-enable-iconv-codec.patch
        0060-activeqt-handle-win64-in-dumpcpp.patch
        0062-fix-static-build-big-resources.patch
        0063-enable-mingw-schannel-alpn.patch
        0064-fix-mingw-win32-winnt-detection.patch
        0100-fix-relocatable-prefix-staticbuild-v2.patch
        0125-qt5-windeployqt-fixes.patch
        0300-qt-5.8.0-cast-errors.patch
        0302-ugly-hack-disable-qdoc-build.patch
        0304-qtdeclarative-disable-dx12.patch
        0310-fix-assimp-not-found.patch)

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  [[ -d ${srcdir}/${_pkgfqn} ]] && rm -rf ${srcdir}/${_pkgfqn}
  tar -xJf ${srcdir}/${_pkgfile}.tar.xz -C ${srcdir} --exclude=${_pkgfqn}/{qtandroidextras,qtmacextras,qtwayland,qtwebengine,qtx11extras} || true

  cd ${srcdir}/${_pkgfqn}

  # MSYS2 gperf cannot handle \r\n.
  find . -name "*.gperf" -exec dos2unix "{}" \;

  apply_patch_with_msg \
    0000-adjust-qmake-conf-mingw.patch \
    0001-qt-5.8.0-fix-sql-libraries-mingw.patch \
    0002-qt-5.8.0-configure-gcc-before-clang.patch \
    0003-enable-avx-support.patch \
    0004-fix-linking-again-different-static-libs.patch \
    0006-qt-5.3.0-win_flex-replace.patch \
    0007-qt-5.3.0-win32-g-Enable-static-builds.patch \
    0008-qt-5.3.0-win32-g-Add-QMAKE_EXTENSION_IMPORTLIB-defaulting-to-.patch \
    0011-qt-5.8.0-mingw-dbus-and-pkg-config.patch \
    0016-qt-5.8.0-win32-g++-use-qpa-genericunixfontdatabase.patch \
    0017-qt-5.3.0-fix-examples-building.patch \
    0025-qt-5.8.0-force-using-make-on-msys.patch

  # Ossi says we should fix this in qwebkit with a $$(shell_path), but the ' quotes get in the way
  # due to qtPrepareTool adding them with $$shell_quote
  apply_patch_with_msg \
    0028-qt-5.8.0-Revert-untangle-use-of-system-vs.-shell-path-list-se.patch \
    0029-qt-5.8.0-Revert-fix-quoting-and-path-separators-in-qtPrepareT.patch \
    0030-qt-5.3.1-workaround-ansidecl-h-PTR-define-conflict.patch \
    0035-qt-5.3.2-dont-add-resource-files-to-qmake-libs.patch

  # Patches so that qt5-static can be used with cmake.
  apply_patch_with_msg \
    0036-qt-5.3.2-win32-qt5-static-cmake-link-ws2_32-and--static.patch

  apply_patch_with_msg \
    0048-qt-5.4.2-win32-Avoid-platformNativeInterface-segfaults-with-minimal-platform.patch \
    0049-qt-5.8.0-win32-do-not-use-fontconfig.patch \
    0051-qt-5.9.1-disable-qtlocation-mapboxgl-plugin.patch \
    0052-qt-5.11-mingw-fix-link-qdoc-with-clang.patch \
    0054-win32-dont-need-flatpack-theme.patch \
    0056-qt-5.11-static_icu.patch \
    0057-fix-and-enable-iconv-codec.patch \
    0060-activeqt-handle-win64-in-dumpcpp.patch \
    0062-fix-static-build-big-resources.patch \
    0063-enable-mingw-schannel-alpn.patch \
    0064-fix-mingw-win32-winnt-detection.patch \
    0100-fix-relocatable-prefix-staticbuild-v2.patch \
    0125-qt5-windeployqt-fixes.patch \
    0300-qt-5.8.0-cast-errors.patch \
    0304-qtdeclarative-disable-dx12.patch \
    0310-fix-assimp-not-found.patch

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    sed -i "s| -mthreads||g" qtbase/mkspecs/common/g++-win32.conf
  fi

  local _ARCH_TUNE=
  case ${MINGW_CHOST} in
    i686*)
      _ARCH_TUNE="-march=pentium4 -mtune=generic"
    ;;
    x86_64*)
      _ARCH_TUNE="-march=nocona -msahf -mtune=generic"
    ;;
  esac

  BIGOBJ_FLAGS="-Wa,-mbig-obj"

  # Append these ones ..
  sed -i "s|^QMAKE_CFLAGS .*= \(.*\)$|QMAKE_CFLAGS            = \1 ${_ARCH_TUNE} ${BIGOBJ_FLAGS} ${LTCG_CFLAGS}|g" qtbase/mkspecs/${_platform}/qmake.conf
  sed -i "s|^QMAKE_LFLAGS           +=\(.*\)$|QMAKE_LFLAGS            += \1 ${LTCG_LFLAGS}|g" qtbase/mkspecs/common/gcc-base.conf
  # But overwrite this one.
  #sed -i "s|^QMAKE_LFLAGS_RELEASE .*= \(.*\)$|QMAKE_LFLAGS_RELEASE    = ${QMAKE_LFLAGS_RELEASE}|g" qtbase/mkspecs/common/g++-win32.conf

  # To keep the build folder name quite small (PATH_MAX limit)
  # the source folder (long) is renamed to $MSYSTEM (MINGW32 or MINGW64)
  cd ${srcdir}
  [[ -d ${MSYSTEM} ]] && rm -rf ${MSYSTEM}
  sleep 5
  mv ${_pkgfqn} ${MSYSTEM}
}

build() {
  local _buildpkgdir=${pkgdirbase}/${pkgname}/${MINGW_PREFIX}/${_realname}
  mkdir -p ${_buildpkgdir}
  local QTDIR_WIN=$(cygpath -am ${_buildpkgdir})

  cd ${MSYSTEM}
  touch qtbase/.gitignore

  # https://github.com/msys2/MSYS2-packages/issues/2282
  export MSYS2_ARG_CONV_EXCL='--foreign-types='

  # Qt manages the compiler flags for release / debug configs separately, so having our own values (-O2) is harmful here ..
  unset CFLAGS
  unset CXXFLAGS
  unset LDFLAGS
  unset QMAKESPEC
  unset XQMAKESPEC
  unset QMAKEPATH
  unset QMAKEFEATURES
  # export NINJA_PATH=${MINGW_PREFIX}/bin/ninja.exe

  local -a configure_opts

  export PATH="${srcdir}/${MSYSTEM}/qtbase/bin:${srcdir}/${MSYSTEM}/qtbase/lib:${PATH}"
  export LLVM_INSTALL_DIR=${MINGW_PREFIX}
  export VULKAN_SDK=${MINGW_PREFIX}

  export QDOC_USE_STATIC_LIBCLANG=1
  #export FORCE_MINGW_QDOC_BUILD=1
  # Hack to disable building qdoc
  patch -p1 -i ${srcdir}/0302-ugly-hack-disable-qdoc-build.patch
  export QDOC_SKIP_BUILD=1

  configure_opts+=("-static")
  configure_opts+=("-static-runtime")
  configure_opts+=("-D" "JAS_DLL=0")
  configure_opts+=("-schannel")

  configure_opts+=("-no-fontconfig")
  configure_opts+=("-no-glib")
  configure_opts+=("-no-gstreamer")
  configure_opts+=("-no-iconv")
  configure_opts+=("-no-icu")
  configure_opts+=("-no-jasper")
  configure_opts+=("-no-mng")
  configure_opts+=("-no-wmf")
  configure_opts+=("-qt-assimp")
  configure_opts+=("-qt-doubleconversion")
  configure_opts+=("-qt-freetype")
  configure_opts+=("-qt-harfbuzz")
  configure_opts+=("-qt-libjpeg")
  configure_opts+=("-qt-libpng")
  configure_opts+=("-qt-pcre")
  configure_opts+=("-qt-tiff")
  configure_opts+=("-qt-webp")
  configure_opts+=("-dbus-linked")
  configure_opts+=("-nomake" "examples")
  configure_opts+=("-nomake" "tests")

  # static can't use anything but -qt-sql-sqlite both for size
  # reasons and because shared libraries get auto-detected and
  # built as plugins.
  local -a _sql_config
  _sql_config+=("-no-sql-ibase")
  _sql_config+=("-no-sql-mysql")
  _sql_config+=("-no-sql-odbc")
  _sql_config+=("-no-sql-psql")
  _sql_config+=("-no-sql-sqlite2")
  _sql_config+=("-sql-sqlite")
  _sql_config+=("-qt-sqlite")

  ./configure \
    -prefix ${QTDIR_WIN} \
    -datadir ${QTDIR_WIN}/share/qt5 \
    -archdatadir ${QTDIR_WIN}/share/qt5 \
    -opengl desktop \
    -opensource \
    -confirm-license \
    -platform ${_platform} \
    -release \
    -feature-relocatable \
    "${configure_opts[@]}" \
    "${_sql_config[@]}"

  # I'm keeping this around while qt5-static cmake problems persist (hopefully not long!)
  # qtbase/mkspecs/modules-inst/qt_lib_core.pri
  # pushd src/x86_64
  #   ROOT=$(pwd -W)
  #   pushd qtbase/
  #     ${ROOT}/qtbase/bin/qmake.exe ${ROOT}/qtbase/qtbase.pro -o Makefile
  #   popd
  #   cp -rf qtbase/src/corelib qtbase/src/corelib.pre-write-cmake
  #   pushd qtbase/src/corelib
  #     ${ROOT}/qtbase/bin/qmake.exe ${ROOT}/qtbase/src/corelib/corelib.pro -d -d -d -d -o Makefile > log.txt
  #   popd
  # popd
  # .
  # .. or .. (this alternative could be used when _minimal=yes)
  # .
  # pushd src/x86_64
  #   make module-qtbase && make module-qtbase-install_subtargets
  # popd
  # .. do your testing.
  # There is a race condition / dependency problem with libqtiff and dumpdoc.exe which
  # can happen at -j9 so make twice (dumpdoc.exe depends on libqtiff but can get built
  # first).
  make || make
}

check() {
  cd ${MSYSTEM}
  make check -j1 -k
}

package() {
  cd ${MSYSTEM}

  make install

  install -D -m644 LICENSE.FDL     "${pkgdir}${MINGW_PREFIX}/${_realname}"/share/licenses/qt5/LICENSE.FDL
  install -D -m644 LICENSE.GPLv2   "${pkgdir}${MINGW_PREFIX}/${_realname}"/share/licenses/qt5/LICENSE.GPLv2
  install -D -m644 LICENSE.GPLv3   "${pkgdir}${MINGW_PREFIX}/${_realname}"/share/licenses/qt5/LICENSE.GPLv3
  install -D -m644 LICENSE.LGPLv21  "${pkgdir}${MINGW_PREFIX}/${_realname}"/share/licenses/qt5/LICENSE.LGPLv21
  install -D -m644 LICENSE.LGPLv3  "${pkgdir}${MINGW_PREFIX}/${_realname}"/share/licenses/qt5/LICENSE.LGPLv3
  install -D -m644 LICENSE.QT-LICENSE-AGREEMENT  "${pkgdir}${MINGW_PREFIX}/${_realname}"/share/licenses/qt5/LICENSE.QT-LICENSE-AGREEMENT

  # Remove dlls from lib/
  rm -f "${pkgdir}${MINGW_PREFIX}/${_realname}/lib"/*.dll

  # Remove *.orig files
  find "${pkgdir}${MINGW_PREFIX}/${_realname}" -type f -name "*.orig" -exec rm -f {} \;

  # Workaround for installing empty .pc files
  plain "---> Fix pkgconfig files..."
  local _pc_files=( $(find ${srcdir}/${MSYSTEM} -type f -name Qt5*.pc) )
  cp -f ${_pc_files[@]} ${pkgdir}${MINGW_PREFIX}/${_realname}/lib/pkgconfig/

  # Fix paths in qconfig.pri and qmodule.pri:
  #   $(cygpath -m ${MINGW_PREFIX}) -> $(cygpath -m ${pkgdir}${MINGW_PREFIX})
  local QT_PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX}/${_realname})
  local PKGDIR_QT_PREFIX_WIN=$(cygpath -m ${pkgdir}${MINGW_PREFIX}/${_realname})
  local FAKE_PREFIX_FOR_REPLACE="@@QT_REAL_PREFIX/dir@@"
  local MINGW_PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX})

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/share/qt5" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${QT_PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/lib" -type f \( -name '*.pri' -o -name '*.prl' \) \
      -exec sed -i -e "s|${QT_PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/lib" -type f -name '*.cmake' \
      -exec sed -i -e "s|${QT_PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${QT_PREFIX_WIN}|${MINGW_PREFIX}/${_realname}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/lib/pkgconfig" -type f -name '*.pc' \
      -exec sed -i -e "s|${PKGDIR_QT_PREFIX_WIN}|${MINGW_PREFIX}/${_realname}|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/lib" -type f -name '*.cmake' \
    -exec sed -i -e "s|${MINGW_PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}/..|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/share/qt5" -type f \( -name '*.pri' -o -name '*.prl' \) \
    -exec sed -i -e "s|${MINGW_PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}/..|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/lib" -type f \( -name '*.pri' -o -name '*.prl' \) \
    -exec sed -i -e "s|${MINGW_PREFIX_WIN}|${FAKE_PREFIX_FOR_REPLACE}/..|g" {} \;

  find "${pkgdir}${MINGW_PREFIX}/${_realname}/lib/pkgconfig" -type f -name '*.pc' \
    -exec sed -i -e "s|${MINGW_PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
}

sha256sums=('26d5f36134db03abe4a6db794c7570d729c92a3fc1b0bf9b1c8f86d0573cd02f'
            '884b32f0e2e1bb110330a921b69443379a7be98c42f13754f9c5f56c040ba3b7'
            '617e6fa85a92353d0073425d37cd5d90d92cb7f906d66dd2d0df576122d091a4'
            '27ce2161b5dbea2fcce6b15948ab722a71036d056420854f6554969f683583bb'
            '15c9fdf4d54628e64a9b7e456a5308aecf0f1c8e33a5aff0ab0bcac0300a9568'
            'e04033364f5ad77906b4100c34572df57bd793a55bc33b7d8a753a2cc60af259'
            '18fd2fa42215ac47b3b314261ab98cbe65f8231429e4f29a152288a3ca93daf1'
            '4e154fbc9059a096c351d019da6b18c907b1d8b06e028f48c7365f62bcd0edc9'
            'e2ec5e67bdcfd162a82c49181a2d480f4b193acdfa6b0e22f4a8448286162630'
            '465d3897c4494695b66ac121944b3bd370db91bfea71dd2c9170dbf268cc99ea'
            'b31929c78f9906756a5e1bdf9a796541865c474aaa1b3eefea1a3f1d7c8d94ca'
            'd8a476be7e55e8bb9362868073dc9e7937431e68b9578c0a0ed103cf1ebc8c01'
            'd7c228df3cc680793a6c858e1fac01092c0877071c075804b7e93f6b3a481eae'
            '97fcd6bd58ff8b8d735a6381b607bb3b4b351d1d07be1bbc18b97155dcbb501d'
            'f80ce415f25f0aa4ead2efe369b41efe35d9848d4d36d8dbf3831e48dbd6e506'
            '19566c38bd50581acaa6a9f46f0e25c41fdfc8b0b42269126e4d3a9b83e2f224'
            'ddaa067d144f5788ab7a3ed0dfc426d702c13de9c35f04eaaf38d642be4934d4'
            '8beeb610df3aff14bae43c56e5781cda3d49f1ad23c6e3fc2aa2405ce002de46'
            'f008164ef2a224b8c52736bab53f1aeec72d610928f03cce57f632824dfdd615'
            'a09ba083957a2b9020b5e17e57db98c3f41390db26d85b310798597fc2a4d34f'
            '42724bd154ed98c612d19a7daee2b5028270ef6dbfe7f35d5f97c8d0605f9fe6'
            '3dc4d7c4fed8fedd726211edbfe13f4d78247d3ab2f789f3ce2a8e43e59a7c5b'
            'c84cb5ba4243413232bc693fb0f54c0283bf0e496e6790a2698f226f80275a61'
            '69e1d6f8fc59ee2da0abe0a6965d0d7730bd015c5b1f1c5b4a69435328b21768'
            '00e32052ac1a9a8391275dfe69901d4ae146ac1441c00bf46f3ef1cfe459a78d'
            '8a74fab12c9d3faac81d75ea1c0662624b83059631db8d9f2570b414d62dc692'
            'b878680172290ad2dcb5508827ef5a9230cbfd8e97d2672d2e96b5addbd55833'
            '928a8eaca8840ff353cdff8fd11140724cb89094e6115fdd7b8cf534830adcca'
            'f07ff4f72626b91aa67ce7ffc78ce7c7c5765bf5431a65b7d867b5f66fa1506b'
            'f930338562119f542b550e3cdb802f1f7ebd476f54dd7297db424271167394b0'
            '6fc871c9a3697666c19d5c21a8f038b80c9c825b418435b0723bf35a9e532d91'
            'cf8b243128bd327971373fe9869350198c5822ecd797d2908bd1a32b62c6907e'
            '28ad823dc8b6665de7b552e82411a8b7b25bf716c42597d8dc8644bded63199c'
            '9ea7aeb486023f3aebf1af44603ebd9266009aaeaf91e52c37c8c5154ea3900c'
            '7e57ce20b6707bc3b4a727da3287cadda13a74317d86515358b2535bc15244c7'
            '052c7035ad9a4fc0321975ba7658fb755bbc0841c7e1d9e88bb0a15e6a90b770'
            '157b97b3e3031d93d93e45368a1c54ebfbf9578116f75f5126e977e5dd518191')
